package io.renren.modules.shop.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.shop.entity.*;
import io.renren.modules.shop.service.CarService;
import io.renren.modules.shop.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shop.service.OrderService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author Yangqianyong
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 15:35:51
 */
@RestController
@RequestMapping("shop/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private CarService carService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("shop:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/detail")
//    @RequiresPermissions("shop:order:list")
    public R list(@RequestParam Map<String, Object> params,@RequestParam Long id){
        PageUtils page = orderService.queryPage(params,id);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
//    @RequiresPermissions("shop:order:info")
    public R info(@PathVariable("orderId") Long orderId){
		OrderEntity order = orderService.getById(orderId);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("shop:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("shop:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("shop:order:delete")
    public R delete(@RequestBody Long[] orderIds){
		orderService.removeByIds(Arrays.asList(orderIds));

        return R.ok();
    }
    @RequestMapping("/addOrder")
    public R addOrder(@RequestParam Long uid,@RequestParam Long pid){
        ProductDetailEntity product = productService.getDetailById(pid);

        OrderEntity order = new OrderEntity();
        order.setCustomerId(uid);
        order.setProductName(product.getProductName());
        order.setMerchantId(product.getCommercialTenantId());
        order.setOrderState(1);
        order.setProductId(pid);
        order.setProductPrice(product.getProductPromoti());
        order.setProductNum(1);
        order.setCreateTime(new Date());
        order.setProductImg(product.getProductImg());

        ProductEntity productEntity = new ProductEntity();
        productEntity.setProductStock(product.getProductStock()-1);
        productService.saveOrUpdate(productEntity);
        orderService.save(order);
        return R.ok();
    }

    @RequestMapping("/addOrderByCars")
    public R addOrder(@RequestBody OrderProductsEntity products){
        List<ProductIdEntity> list = products.getList();
        Long userId = products.getUserId();
        List<ProductDetailEntity> list1 = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            ProductDetailEntity productDetailEntity = productService.getDetailById(list.get(i).getProductId());
            list1.add(i,productDetailEntity);
            System.out.println(list.get(i).toString());
        }
        List<ShoppingCarsEntity> list2 = new ArrayList<>();
//        删除购物车相应的数据
        for(int i=0;i<list.size();i++) {
            ShoppingCarsEntity carEntity = carService.getShoppingCart(userId, list.get(i).getProductId());
            System.out.println(carEntity.toString());
            list2.add(i, carEntity);
            carService.removeById(carEntity.getId());
        }
//        添加订单
        for(int i=0;i<list.size();i++){
            OrderEntity order = new OrderEntity();
            order.setCustomerId(userId);
            order.setProductName(list2.get(i).getProductName());
            order.setMerchantId(list1.get(i).getCommercialTenantId());
            order.setOrderState(1);
            order.setProductId(list2.get(i).getProductId());
            order.setProductPrice(list2.get(i).getProductPromoti());
            order.setProductNum(list2.get(i).getNum());
            order.setCreateTime(new Date());
            order.setProductImg(list1.get(i).getProductImg());
//            查找商品表库存数据并减去订单数量
//            ProductEntity productEntity = productService.getById(list1.get(i).getProductId());
//            if(productEntity.getProductStock()-list2.get(i).getNum()>=0)
//                productEntity.setProductStock(productEntity.getProductStock()-list2.get(i).getNum());
//            else
//                return R.error("内存不足");

            orderService.save(order);
//            productService.saveOrUpdate(productEntity);
        }

        return R.ok();
    }

    @RequestMapping("/changeState")
    public R changeState(@RequestParam Long id){
        OrderEntity order = orderService.getById(id);
        order.setOrderState(0);
        orderService.saveOrUpdate(order);
        return R.ok();
    }
    @RequestMapping("/uOrder")
    public R orderZt(@RequestParam Long uid,@RequestParam Integer orderState){

        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderEntity::getCustomerId,uid);
        queryWrapper.lambda().eq(OrderEntity::getOrderState,orderState);
        List<OrderEntity> list = orderService.list(queryWrapper);

        return R.ok().put("list",list);
    }

    @RequestMapping("/mOrder")
    public R orderZtS(@RequestParam Long mid,@RequestParam Integer orderState){
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderEntity::getMerchantId,mid);
        queryWrapper.lambda().eq(OrderEntity::getOrderState,orderState);
        List<OrderEntity> list = orderService.list(queryWrapper);

        return R.ok().put("list",list);
    }


}
